﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Booking.Service
{
    public class SalaryService
    {
        /*
         * 1.新增
         * 2.修改
         * 3.删除
         * 4.判断是否有重复项
         * 5.按时间段查询
         */
        public async Task<bool> AddAsync( Salary.Salary salary)
        {
            using (var context = new BookingContext())
            {
                context.Salarys.Add(salary);
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;
            }


        }
        public async Task<bool> UpdateAsync(Salary.Salary salary)
        {
            using (var context = new BookingContext())
            {
                var exitItem = context.Salarys.Single(s => s.Id == salary.Id);
                exitItem.Amount = salary.Amount;
                exitItem.Inserted = salary.Inserted;

                exitItem.Item = salary.Item;
                exitItem.Time = salary.Time;
                exitItem.Validation = salary.Validation;
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;


            }

        }
        public async Task<bool> DeleteAsync(int id)
        {
            using (var context = new BookingContext())
            {
                var item = context.Salarys.Single(s => s.Id == id);
                item.Validation = false;
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;
            }

        }
        public async Task<bool> QueryNameExist(string Name)

        {
            using (var context = new BookingContext())
            {
                return await context.Salarys.AnyAsync(s => s.Item.Name == Name);

            }

        }
        public async Task<List<Salary.Salary>> QueryAsync(DateTime start,DateTime end)
        {
                using (var context = new BookingContext())
                {
                return await context.Salarys.Where(s => s.Time >= start).Where(s => s.Time <= end).ToListAsync();
                }

        }

    }
}
